'''
Created on 2010-2-12

@author: summit
'''

import sys
from jolly.datastructures.Object import Object

class Association(Object):
    '''
    An association is an ordered pair of objects. The first element of the pair 
    is called the key ; the second element is the value  associated with the 
    given key
    '''


    def __init__(self, *args):
        '''
        we implement an association using a Python tuple 
        '''
        if len(args) == 1:
            self._tuple = (args[0], None)
        elif len(args) == 2:
            self._tuple = args
        else:
            raise ValueError
        
    def getKey(self):
        return self._tuple[0]
    key = property(fget=lambda self: self.getKey())
    
    def getValue(self):
        return self._tuple[1]
    value = property(fget=lambda self: self.getValue())
    
    def _compareTo(self, assoc):
        assert isinstance(self, assoc.__class__)
        return cmp(self.key, assoc.key)
    
    def __str__(self):
        return "Association %s" % str(self._tuple)
    
    def __hash__(self):
        """
        (Association) -> int
        Hashes the key of this association.
        """
        return hash(self.key)


    @staticmethod
    def main(*argv):
        "Association test program. "
        print Association.main.__doc__
        a = Association(1,2)
        print a, a.key, a.value
        print hash(a)
        print Association(2,2) > Association(3,2)
        b = Association(3)
        print b
        return 0

if __name__ == "__main__":
    sys.exit(Association.main(*sys.argv))